package com.ebook.ui.fragment.home.shuffle;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class TranslucentLabel extends JLabel {
    BufferedImage labelImage = null;
    public TranslucentLabel(String label){
        super(label,SwingConstants.CENTER);
        setOpaque(true);
        setBackground(Color.GRAY);
    }
    public void paint(Graphics g){
        if (labelImage == null || labelImage.getWidth() != getWidth() ||
                labelImage.getHeight() != getHeight()) {
            labelImage = getGraphicsConfiguration().
                    createCompatibleImage(getWidth(), getHeight());
        }
        Graphics gLabel = labelImage.getGraphics();
        gLabel.setClip(g.getClip());

        //  Have the superclass render the button for us
        super.paint(gLabel);

        // Make the graphics object sent to this paint() method translucent
        Graphics2D g2d  = (Graphics2D)g;
        AlphaComposite newComposite =
                AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
        g2d.setComposite(newComposite);

        // Copy the button's image to the destination graphics, translucently
        g2d.drawImage(labelImage, 0, 0, null);
    }
}
